//
//  jxsTask.m
//  ToDoList
//
// This code file is apart of project "iPhone-ToDo-List" 
// (http://code.google.com/p/iphone-todo-list/)
//
// Copyright (c) 2009 Frank Villasenor
//
// Distributed under the terms of the Lesser GNU General Public License.
// A copy of the license should be with these files. If not, you 
// can retrieve a copy from http://www.gnu.org/licenses/lgpl-3.0.txt
// 

#import "tosuTask.h"


@implementation tosuTask

@synthesize title;
@synthesize dueDate;
@synthesize isComplete;
@synthesize notes;
@synthesize subTasks;

- (void)dealloc 
{
	[title release];
	[dueDate release];
	[notes release];
	[subTasks release];
	
    [super dealloc];
}

- (tosuTask *)init
{
	self = [super init];
	if( self )
	{
		NSString *localTitle = [[NSString alloc] init];
		self.title = localTitle;
		[localTitle release];
		
		NSDate *tempDueDate = [[NSDate alloc] init];
		self.dueDate = tempDueDate;
		[tempDueDate release];
		
		self.isComplete = NO;
		
		NSString *tempNotes = [[NSString alloc] init];
		self.notes = tempNotes;
		[tempNotes release];
		
		self.subTasks = nil;
	}
	
	return self;
}

- (tosuTask *)initWithTaskTitle:(NSString *)pTitle DueDate:(NSDate *)pDueDate 
					IsComplete:(BOOL)pIsComplete Notes:(NSString *)pNotes
{
	self = [super init];
	
	if( self )
	{
		self.title = pTitle;
		self.dueDate = pDueDate;
		self.isComplete = pIsComplete;
		self.notes = pNotes;
		self.subTasks = nil;
	}
	
	return self;
}

- (tosuTask *)initWithTaskTitle:(NSString *)pTitle DueDate:(NSDate *)pDueDate 
					IsComplete:(BOOL)pIsComplete Notes:(NSString *)pNotes 
				 subTasksArray:(NSMutableArray *)pSubTasks
{
	self = [self initWithTaskTitle:pTitle DueDate:pDueDate IsComplete:pIsComplete Notes:pNotes];
	
	if( self )
	{
		self.subTasks = pSubTasks;
	}
	return self;
}

- (NSUInteger)hash
{
	NSUInteger titleHash = [self.title hash];
	NSUInteger dueDateHash = [self.dueDate hash];
	NSUInteger notesHash = [self.notes hash];
	NSUInteger subTasksHash = [self.subTasks hash];
	
	return ((titleHash + dueDateHash + notesHash + subTasksHash) / 4);
}

#pragma mark -
#pragma mark NSCoding Methods
- (id)initWithCoder:(NSCoder *)decoder
{
	//
	// Code based on Chp11; pg 342
	if( self = [super init] )
	{
		self.title = [decoder decodeObjectForKey:kTitleKey];
		self.dueDate = [decoder decodeObjectForKey:kDueDateKey];
		self.isComplete = [decoder decodeBoolForKey:kIsCompleteKey];
		self.notes = [decoder decodeObjectForKey:kNotesKey];
		self.subTasks = [decoder decodeObjectForKey:kSubTasks];
	}
	
	return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
	//
	// Code based on Chp11; pg 342
	[encoder encodeObject:self.title forKey:kTitleKey];
	[encoder encodeObject:self.dueDate forKey:kDueDateKey];
	[encoder encodeBool:self.isComplete forKey:kIsCompleteKey];
	[encoder encodeObject:self.notes forKey:kNotesKey];
	[encoder encodeObject:self.subTasks forKey:kSubTasks];
}

#pragma mark -
#pragma mark NSCopying Methods
- (id)copyWithZone:(NSZone *)zone
{
	//
	// Code based on Chp11; pg 343.
	tosuTask *localTask = [[[self class] allocWithZone:zone] init];
	
	NSString *localTitle = [self.title copy];  //copy is a convienace method and it retains the returned object. 
	localTask.title = localTitle; //this also retains (setTitle)
	[localTitle release]; //release the 'copy' created object.
	
	NSDate *localDueDate = [self.dueDate copy];
	localTask.dueDate = localDueDate;
	[localDueDate release];
	
	localTask.isComplete = self.isComplete;
	
	NSString *localNotes = [self.notes copy];
	localTask.notes = localNotes;
	[localNotes release];
	
	NSMutableArray *localSubTasks = [self.subTasks copy];
	localTask.subTasks = localSubTasks;
	[localSubTasks release];
	
	return localTask;
}
@end

